<template>
  <swiper :options="swiperOptions">
    <!-- slides -->
    <swiper-slide v-for="(iconArr, index) of pages" :key="index">
      <div class="icons">
        <div class="icon" v-for="item of iconArr" :key="item.imgId">
          <div class="img_wrap">
            <img :src="item.imgUrl">
          </div>
          <p class="icon_desc">{{item.desc}}</p>
        </div>
    </div>
    </swiper-slide>
  </swiper>
</template>

<script>

export default {
  name: 'HomeIcon',
  props: {
    iconList: Array
  },
  data () {
    return {
      swiperOptions: {
        setWrapperSize: true,
        paginationClickable: true,
        mousewheelControl: false,
        observeParents: true,
        loop: false
      }
    }
  },
  // 利用计算属性计算icon总共应该显示几页
  computed: {
    // page返回页数的数组
    pages () {
      // 定义一个用来存页的数组
      const pages = []
      // 遍历每一个icon用来确定放在那个数组里
      this.iconList.forEach((item, index) => {
        // 通过下表计算当前icon应该再第几页
        let pageIndex = Math.floor(index / 8)
        if (!pages[pageIndex]) {
          pages[pageIndex] = []
        }
        pages[pageIndex].push(item)
      })
      return pages
    }
  }
}
</script>
<style lang="stylus" scoped>
@import'~styles/varibles.styl'
@import'~styles/mixins.styl'
  .icons
    margin-top .1rem
    width 100%
    height 0
    overflow hidden
    padding-bottom 50%
    background #fff
    .icon
      position relative
      float left
      width 25%
      height 0
      padding-bottom 25%
      overflow hidden
      .img_wrap
        position absolute
        top 0
        left 0
        right 0
        bottom .44rem
        box-sizing border-box
        padding .1rem
        img
          display block
          margin 0 auto
          height 100%
      .icon_desc
        position absolute
        color $darkTextColor
        left 0
        right 0
        bottom 0
        height .44rem
        line-height.44rem
        text-align center
        ellipsis()
</style>
